package com.baidu.videoseries.fragment;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.baidu.videoseries.R;
import com.baidu.videoseries.activity.VlcActivity;
import com.baidu.videoseries.adapter.SimpleItemAdapter;
import com.baidu.videoseries.constant.Config;
import com.baidu.videoseries.util.PageUtil;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.system.PermissionUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    @BindView(R.id.itemsLv)
    ListView itemsLv;
    Unbinder unbinder;

    private ArrayList<String> data;
    private SimpleItemAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        data = new ArrayList<>();
        adapter = new SimpleItemAdapter(data, getActivity());
        itemsLv.setAdapter(adapter);
        itemsLv.setOnItemClickListener(this);

        initData();

        //request permission
        requestPer();
    }

    /**
     * request permission
     */
    private void requestPer() {
        String[] per = new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,

        };

        if (!PermissionUtils.isPermitted(getActivity(), per)) {
            PermissionUtils.requestPermission(getActivity(), per, 5);
        }
    }

    private void initData() {
        if (data == null) return;
        data.add(Config.Tags.SYS_PLAYER);
        data.add(Config.Tags.VIDEO_VIEW);
        data.add(Config.Tags.MEDIA_PLAYER);
        data.add(Config.Tags.AUDIO);
        data.add(Config.Tags.VLC_PLAYER);
        data.add(Config.Tags.TEST);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ToastUtils.simpleToast(getActivity(), "your choice is " + position);
        String tag = data.get(position);
        switch (tag) {
            case Config.Tags.SYS_PLAYER:
                jump(PageUtil.Index.SYS_PLAYER);
                break;
            case Config.Tags.VIDEO_VIEW:
                jump(PageUtil.Index.VIDEO_VIEW);
                break;
            case Config.Tags.MEDIA_PLAYER:
                jump(PageUtil.Index.MEDIA_PLAYER);
                break;
            case Config.Tags.AUDIO:
                jump(PageUtil.Index.AUDIO);
                break;
            case Config.Tags.VLC_PLAYER:
//                JumpUtils.simpleJump(getActivity(), VlcActivity.class);
                jump(PageUtil.Index.VLC);
                break;
            case Config.Tags.TEST:
                jump(PageUtil.Index.TEST);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 5) {
            boolean isPer = true;
            for (int i = 0; i < grantResults.length; i++) {
                isPer = isPer && (grantResults[i] == PackageManager.PERMISSION_GRANTED);
            }
            if (!isPer) {
                getActivity().finish();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }
}
